﻿Imports System.ComponentModel
Imports System.ComponentModel.Design

<DefaultEvent("GroupSelectEvent")> _
Public Class CcapDevTypeControl

    Private _pi As ProjectItem
    Public Shared CCAP_AIRCOOLER_NUM As Integer
    Public Shared CCAP_CONDENSER_NUM As Integer
    Public Shared CCAP_COMPRESSORSET_NUM As Integer
    Public Shared CCAP_COMPRESSOR_NUM As Integer


    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Init()

    End Sub
        Private Sub Init()
        Me.tb_netid.Text = "50"
        'Me.rb_DevTypeBig.Checked = True
        'Me.rb_DevTypeSmall.Checked = False
        Me.CombDevType.SelectedIndex = -1


        _groupSelected = False
    End Sub

   
    Private _dtiItem As CcapDevItem
    <Browsable(False)> _
    Public Property dtiItem() As CcapDevItem
        Get
            getItem()
            If _dtiItem Is Nothing Then
                _dtiItem = New CcapDevItem()
            End If

            Return _dtiItem

        End Get
        Set(ByVal value As CcapDevItem)
            _dtiItem = value
            SetItem(value)
        End Set
    End Property

    Private Sub getItem()

        If _dtiItem Is Nothing Then
            _dtiItem = New CcapDevItem()
        End If


        _dtiItem.ProjectID = MainForm.pi.ProjectID
        _dtiItem.NetId = Integer.Parse(Trim(Me.tb_netid.Text))
        '  _dtiItem.DevType = IIf(Me.rb_DevTypeBig.Checked, 1, 2)
        _dtiItem.DevType = Me.CombDevType.SelectedIndex + 1
        '_dtiItem.Id = GetAvailid(MainForm.pi.ProjectID)
        Select Case _dtiItem.DevType
            Case 1
                CCAP_AIRCOOLER_NUM = 8 '= 8   '每设备最多配置冷风机数量 8
                CCAP_CONDENSER_NUM = 8 '= 8  '每设备最多配置冷凝器数量 8
                CCAP_COMPRESSORSET_NUM = 4 '= 4 '每设备最多配置机组数量 4
                CCAP_COMPRESSOR_NUM = 6
            Case 2
                CCAP_AIRCOOLER_NUM = 8 '= 8   '每设备最多配置冷风机数量 8
                CCAP_CONDENSER_NUM = 8 '= 8  '每设备最多配置冷凝器数量 8
                CCAP_COMPRESSORSET_NUM = 8 '= 8 '每设备最多配置机组数量 8
                CCAP_COMPRESSOR_NUM = 6

        End Select

        
        _dtiItem.CompressorSetGroupList = New CcapDevItem().CompressorSetGroupList
        _dtiItem.AirCoolerGroupList = New CcapDevItem().AirCoolerGroupList
        _dtiItem.CondenserGroupList = New CcapDevItem().CondenserGroupList

        Dim rti As CcapCompressorSetItem
        For i As Integer = 0 To Me.FlowLayoutPanel1.Controls.Count - 1
            rti = CType(Me.FlowLayoutPanel1.Controls(i), CcapComTypeControl).refItem
            _dtiItem.CompressorSetGroupList.Add(rti)


        Next

        Dim condenser As CcapCondenserItem

        For i As Integer = 0 To Me.FlowLayoutPanel2.Controls.Count - 1
            condenser = CType(Me.FlowLayoutPanel2.Controls(i), CcapcondenserTypeItem).CondenserItem
            _dtiItem.CondenserGroupList.Add(condenser)


        Next

        Dim aircooler As CcapAirCoolerItem

        For i As Integer = 0 To Me.FlowLayoutPanel3.Controls.Count - 1
            aircooler = CType(Me.FlowLayoutPanel3.Controls(i), CcapAirCoolerTypeItem).AirCoolerItem
            'rti.RefId = i + 1

            _dtiItem.AirCoolerGroupList.Add(aircooler)


        Next

    End Sub
 
  
    Private Sub SetItem(ByVal value As CcapDevItem)
        If value.NetId > 0 And value.NetId <= 256 Then
            Me.tb_netid.Text = value.NetId.ToString
        End If

        'If value.DevType = 1 Then
        '    Me.rb_DevTypeBig.Checked = True
        'ElseIf value.DevType = 2 Then
        '    Me.rb_DevTypeSmall.Checked = True
        'Else
        '    Me.rb_DevTypeBig.Checked = False
        '    Me.rb_DevTypeSmall.Checked = False
        'End If


        Me.CombDevType.SelectedIndex = value.DevType - 1

        For Each item As CcapAirCoolerItem In value.AirCoolerGroupList
            AddCoorairGroupControl(item, value)
        Next

        For Each item As CcapCompressorSetItem In value.CompressorSetGroupList
            AddRefGroupControl(item)
        Next


        For Each item As CcapCondenserItem In value.CondenserGroupList
            AddCondenserGroupControl(item, value)
        Next

    End Sub

    Private Sub CompressorGroupControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        RaiseEvent GroupSelectEvent(Me, New EventArgs)
    End Sub

    Public Event GroupSelectEvent As EventHandler

    Private _groupSelected As Boolean
    Public Property groupSelected() As Boolean
        Get
            Return _groupSelected
        End Get
        Set(ByVal value As Boolean)
            _groupSelected = value
            Me.GroupBox3.Visible = value
        End Set
    End Property


    Public Sub SetEnable(ByVal b As Boolean)
        Me.Panel1.Enabled = b
        Me.TabControl1.Enabled = b

    End Sub

    Public Sub SetNetId(ByVal val As Integer)
        Me.tb_netid.Text = val.ToString()

    End Sub

    Public Sub AddRefGroupControl(ByVal rti As CcapCompressorSetItem)
        Dim c As New CcapComTypeControl(_dtiItem)
        c.refItem = rti
        c.refSelected = False
        c.Panel1.Enabled = False
        c.SetEnable(False)
        AddHandler c.RefSelectEvent, AddressOf CompressorRefControl_RefSelectEvent
        Me.FlowLayoutPanel1.Controls.Add(c)


    End Sub
    Public Sub AddCoorairGroupControl(ByVal rti As CcapAirCoolerItem, ByVal value As CcapDevItem)
        Dim c As New CcapAirCoolerTypeItem(value)
        c.AirCoolerItem = rti
        'For Each rti1 As CcapCompressorSetItem In value.CompressorSetGroupList
        '    c.cmbRefType1.Items.Add(rti)
        'Next

        c.refSelected = False
        c.Panel1.Enabled = False
        AddHandler c.RefSelectEvent, AddressOf CompressorCoorairControl_RefSelectEvent
        Me.FlowLayoutPanel3.Controls.Add(c)


    End Sub

    Public Sub AddCondenserGroupControl(ByVal rti As CcapCondenserItem, ByVal value As CcapDevItem)
        Dim c As New CcapcondenserTypeItem(value)
        c.CondenserItem = rti
        'For Each rti1 As CcapCompressorSetItem In refTypeList
        '    c.cmbRefType1.Items.Add(rti)
        'Next
        c.refSelected = False
        c.Panel1.Enabled = False
        AddHandler c.RefSelectEvent, AddressOf CompressorCondenserControl_RefSelectEvent
        Me.FlowLayoutPanel2.Controls.Add(c)


    End Sub
    Private Sub CompressorCondenserControl_RefSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As CcapcondenserTypeItem In Me.FlowLayoutPanel2.Controls
            c.refSelected = False
            c.Panel1.Enabled = False
        Next

        CType(sender, CcapcondenserTypeItem).refSelected = True
    End Sub
    Private Sub CompressorCoorairControl_RefSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As CcapAirCoolerTypeItem In Me.FlowLayoutPanel3.Controls
            c.refSelected = False
            c.Panel1.Enabled = False
        Next

        CType(sender, CcapAirCoolerTypeItem).refSelected = True
    End Sub
    Private Sub CompressorRefControl_RefSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As CcapComTypeControl In Me.FlowLayoutPanel1.Controls
            c.refSelected = False
            c.Panel1.Enabled = False
        Next

        CType(sender, CcapComTypeControl).refSelected = True
    End Sub
    Public Function GetSelectedControl() As Object
        Dim item As Object = Nothing

        For Each c As CcapComTypeControl In Me.FlowLayoutPanel1.Controls
            If c.refSelected Then
                item = c

                Return item
            End If
        Next


        For Each c As CcapAirCoolerTypeItem In Me.FlowLayoutPanel3.Controls
            If c.refSelected Then
                item = c

                Return item
            End If
        Next

        For Each c As CcapcondenserTypeItem In Me.FlowLayoutPanel2.Controls
            If c.refSelected Then
                item = c

                Return item
            End If
        Next

        Return item
    End Function

    
   
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        ' Select Case Me.TabControl1.SelectedIndex
        ' Case 0
        For Each c As CcapComTypeControl In Me.FlowLayoutPanel1.Controls
            c.refSelected = False
        Next

        For Each c As CcapAirCoolerTypeItem In Me.FlowLayoutPanel3.Controls
            c.refSelected = False
        Next
        For Each c As CcapcondenserTypeItem In Me.FlowLayoutPanel2.Controls
            c.refSelected = False
        Next

        '   Case 1
        '   Case 2


        ' End Select
    End Sub

    Private Sub CombDevType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CombDevType.SelectedIndexChanged
        Select Case Me.CombDevType.SelectedIndex

            Case 0
                CCAP_AIRCOOLER_NUM = 8 '= 8   '每设备最多配置冷风机数量 8
                CCAP_CONDENSER_NUM = 8 '= 8  '每设备最多配置冷凝器数量 8
                CCAP_COMPRESSORSET_NUM = 4 '= 4 '每设备最多配置机组数量 4
                CCAP_COMPRESSOR_NUM = 6
            Case 1
                CCAP_AIRCOOLER_NUM = 8 '= 8   '每设备最多配置冷风机数量 8
                CCAP_CONDENSER_NUM = 8 '= 8  '每设备最多配置冷凝器数量 8
                CCAP_COMPRESSORSET_NUM = 8 '= 4 '每设备最多配置机组数量 4
                CCAP_COMPRESSOR_NUM = 6

        End Select

    End Sub
End Class